package com.aote.rs;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Context;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;

import com.browsesoft.EntityManager;
import com.browsesoft.Tools;
import com.browsesoft.dbtools.DBTools;
import com.browsesoft.resource.LicenseFunctionTreeModel;
import com.browsesoft.resource.Resource;
import com.browsesoft.resource.ResourceTool;
import com.browsesoft.tools.BSCalendar;
import com.browsesoft.user.User;

@Path("/loginuser")
public class UserRoot extends LicenseFunctionTreeModel {

	static Logger log = Logger.getLogger(UserRoot.class);

	@GET
	public String getUser(@Context
	HttpServletRequest request) throws Exception {
		User user = getLoginUser(request);
		String result = "";
		if (user != null) {
			String systemtime = BSCalendar.now().toString("yyyy-mm-dd");
			user.getAttributes().put("systemtime", systemtime);
			result = JSONObject.fromObject(user.getAttributes()).toString();
		} else {
			result = "null";
		}
		return result;
	}

	@SuppressWarnings("unchecked")
	@Path("/root")
	@GET
	public String login(@Context
	HttpServletRequest request) throws Exception {
		User user = getLoginUser(request);
		resources = this.getRes(user);
		notResources = ResourceTool.getAllAncenstor(resources);
		this.allResources.addAll(resources);
		this.allResources.addAll(notResources);
		this.setRoot();
		Object root = this.getRootNode();
		ArrayList list = this.getExpendNode(root);
		String result = JSONArray.fromObject(list).toString();
		System.out.println(result);
		return result;
	}

	// /*
	// * 字符串gbk转化为utf-8
	// */
	// private String Trsfer(String value) {
	// try {
	// value = new String(value.getBytes("utf-8"), "GBK");
	// } catch (UnsupportedEncodingException e) {
	// // TODO Auto-generated catch block
	// e.printStackTrace();
	// }
	// return value;
	// }

	/*
	 * hashtable转化utf-8
	 */
	private Hashtable TrsferHashtable(Hashtable ht) {
		Hashtable result = new Hashtable();
		result.put("name", (String) ht.get("name"));
		result.put("id", (String) ht.get("id"));
		result.put("icon", (String) ht.get("icon"));
		result.put("navigate", (String) ht.get("navigate"));
		return result;
	}

	/*
	 * 扩展节点
	 */
	private ArrayList getExpendNode(Object node) throws Exception {
		ArrayList list = new ArrayList();
		Iterator childiter = ((LinkedList) this.getChildNodes(node)).iterator();
		while (childiter.hasNext()) {
			Object childNodes = childiter.next();
			Hashtable ht = ((Resource) childNodes).getAttributes();
			// ht = TrsferHashtable(ht);
			if (this.getChildNodes(childNodes).size() > 0) {
				ht.put("children", this.getExpendNode(childNodes));
			} else {
				ht.put("leaf", true);
			}
			list.add(ht);
		}
		return list;
	}

	/**
	 * 获得登陆用户
	 * 
	 * @param request
	 * @return
	 */
	public User getLoginUser(HttpServletRequest request) {
		User user = null;
		try {
			// user = (User) request.getSession().getAttribute("loginUser");
			user = (User) EntityManager.getInstance().getUserForLoginName(
					"system");
			System.out.println("用户类型： " + user.getRolesStr());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return user;
	}
}
